const {Commodity} = require('../../models/commodity');
const {User} = require("../../models/User");
const jwt = require('jsonwebtoken');

module.exports = async (req, res) => {
    var followId = req.query.followId;
    var userId = req.query.user_id;
    var isFollow = false;
    const resInfo = await User.findOne({user_id:followId},{"user_id":1,"nickname":1,"avatar":1,"sex":1,
    "summary":1,"follows":1,"fans":1,"collections":1}).lean(); 
 
    //如果已登录
    if(req.headers.authorization){
    const follow = await User.findOne({user_id:userId,"follows.user_id":followId},{"follows.user_id":1});
    if(follow){
        isFollow = true; 
    }
    }

    let opt = [
        {$match:{"user_id":followId}},
        {
            $lookup:{
                from: "books",
                localField: "ISBN",
                foreignField: "ISBN",
                as: "books"
            },
        },
        {$unwind:"$books"},
        {
            $project:{
                "_id":0,
                "artNo":"$Art_No",
                "img":"$books.img",
                "customPrice":1,
                "ISBN":"$books.ISBN",
                "title":"$books.title",
            }
        },
    ]
    Commodity.aggregate(opt,(err,data)=>{
        if(err){
            console.log(err)
            res.status(500).send({errCode:'1',msg:"服务器错误"})
        }else{
            if(data != null){
                resInfo.bookSelled = data;
            }
            resInfo.isFollow = isFollow;
            res.send({errCode:"0",msg:"请求成功",data:{sellerInfo:resInfo}})
        }
    })
}